import Axios from "axios";

const TOKEN_NAME = 'Authorization'
const TOKEN_PREFIX = 'Bearer '

const service = Axios.create({
  baseURL: '',
  timeout: 10000
})

service.defaults.headers['Context-Type'] = 'application/json'

service.interceptors.request.use(config => {
  const isToken = !!config.headers[TOKEN_NAME]
  if(isToken){
    config.headers[TOKEN_NAME] = TOKEN_PREFIX
  }
  return config
}, error => {
  return Promise.reject(error)
})

service.interceptors.response.use(response => {
  const status = response.data.code || response.status
  const message = response.data.message || response.statusText

  if (status === 200 || status === 0) {
    return response.data
  } else {
    return Promise.reject(response)
  }
}, error => {
  return Promise.reject(error)
})

export default service
